package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.WindowConstants;

import view.interfaces.IAddShapeOne;
import controller.ControllerAddShapeOne;
import controller.ControllerHome;

public class AddShapeOne extends JFrame implements IAddShapeOne {
	
	protected JPanel panelTitle, panelFilling, panelOutline, panelEmptyOne, panelEmptyTwo, panelSendButton;
	protected ControllerAddShapeOne thisController;
	protected JComboBox<Color> comboBoxColorFilling;
    protected JComboBox<Color> comboBoxColorOutline;
    protected JButton btnSend;
	
	public AddShapeOne(ControllerAddShapeOne controller) {

		super("Ajouter une forme");
		
		thisController = controller;
        thisController.setView(this);
		
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		
		setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
		
		panelTitle = new JPanel(new FlowLayout());
		panelFilling = new JPanel(new FlowLayout());
		panelOutline = new JPanel(new FlowLayout());
		panelEmptyOne = new JPanel();
		panelEmptyTwo = new JPanel();

		panelSendButton = new JPanel();
		panelSendButton.setLayout(new FlowLayout());
		
		btnSend = new JButton("Ajouter");
		
		panelTitle.add(new JLabel("Ajouter une forme"));
		
		panelFilling.add(new JLabel("Remplissage :"));
    	comboBoxColorFilling = new JComboBox(ControllerHome.colorsAvailable);
    	comboBoxColorFilling.setRenderer(new ColorCellRenderer());
    	panelFilling.add(comboBoxColorFilling);
    	
    	panelOutline.add(new JLabel("Contour :"));
    	comboBoxColorOutline = new JComboBox(ControllerHome.colorsAvailable);
    	comboBoxColorOutline.setRenderer(new ColorCellRenderer());
    	panelOutline.add(comboBoxColorOutline);
    	
    	btnSend = new JButton("Ajouter");
        btnSend.addActionListener(thisController);
        btnSend.setActionCommand("SENDADDSHAPEONE");
    	
    	panelSendButton.add(btnSend);
    	
    	add(panelTitle);
    	add(panelEmptyOne);
    	add(panelFilling);
    	add(panelOutline);
    	add(panelEmptyTwo);
    	add(panelSendButton);
		
		setSize(400,400);

		pack();
		setVisible(true);
		
    }
	
	private class ColorCellRenderer implements ListCellRenderer {
		
		protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
		
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		
		    JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		    
			if (value instanceof Color) {

				renderer.setBackground((Color) value);
				renderer.setText(((Color) value).toString());

			}
			    
			return renderer;
		    
		}
		
	}
	
	public void hideWindowAddShape() {
    	
    	setVisible(false);
    	
    }
	
	public Color getValueComboBoxColorFilling() {
    	
    	return (Color) comboBoxColorFilling.getSelectedItem();
    	
    }
    
    public Color getValueComboBoxColorOutline() {
    	
    	return (Color) comboBoxColorOutline.getSelectedItem();
    	
    }

}
